<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
     <!-- <el-form-item label="优惠卷id" prop="preferentialId">
        <el-input
          v-model="queryParams.preferentialId"
          placeholder="请输入优惠卷id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="优惠卷名" prop="preferentialName">
        <el-input
          v-model="queryParams.preferentialName"
          placeholder="请输入优惠卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠状态" prop="preferentialState">
  <el-select v-model="queryParams.preferentialState" placeholder="请选择优惠状态" clearable @change="handleQuery">
    <el-option label="上架" value="1"></el-option>
    <el-option label="下架" value="2"></el-option>
  </el-select>
</el-form-item>
      <el-form-item label="发行方式" prop="preferentialWay">
        <el-input
          v-model="queryParams.preferentialWay"
          placeholder="请输入优惠卷发行方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="优惠卷数量" prop="preferentialQuantity">
        <el-input
          v-model="queryParams.preferentialQuantity"
          placeholder="请输入优惠卷数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
     <!-- <el-form-item label="开始时间" prop="startTime">
        <el-input
          v-model="queryParams.startTime"
          placeholder="请输入开始时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-input
          v-model="queryParams.endTime"
          placeholder="请输入结束时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠卷剩余数量" prop="preferentialRemainder">
        <el-input
          v-model="queryParams.preferentialRemainder"
          placeholder="请输入优惠卷剩余数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createId">
        <el-input
          v-model="queryParams.createId"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updateId">
        <el-input
          v-model="queryParams.updateId"
          placeholder="请输入更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateDate">
        <el-date-picker clearable
          v-model="queryParams.updateDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker> -->
      <!-- </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['preferential:preferential:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['preferential:preferential:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['preferential:preferential:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['preferential:preferential:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="preferentialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="优惠卷id" align="center" prop="preferentialId" />
      <el-table-column label="优惠卷名称" align="center" prop="preferentialName" />
      <el-table-column label="优惠状态" align="center" prop="preferentialState" :formatter="formatState"/>
      <el-table-column label="优惠卷发行方式" align="center" prop="preferentialWay" />
      <el-table-column label="优惠卷数量" align="center" prop="preferentialQuantity" />
     <!-- <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" /> -->
      <el-table-column label="优惠卷剩余数量" align="center" prop="preferentialRemainder" />
    <!--  <el-table-column label="创建人" align="center" prop="createId" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateId" />
      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['preferential:preferential:edit']"
          >修改</el-button>
		<el-button
		  size="mini"
		  type="text"
		  icon="el-icon-view"
		  @click="aaa(scope.row)"
		>查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['preferential:preferential:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  <!-- 添加或修改优惠卷对话框 -->
<el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
  <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    <el-row :gutter="20">添加优惠卷
      <el-col :span="12">
        <el-form-item label="优惠卷id" prop="preferentialId">
          <el-input v-model="form.preferentialId" placeholder="请输入优惠卷id" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="优惠卷名称" prop="preferentialName">
          <el-input v-model="form.preferentialName" placeholder="请输入优惠卷名称" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="优惠状态" prop="preferentialState">
          <el-select v-model="form.preferentialState" placeholder="请选择优惠状态" style="width: 100%;">
            <el-option label="上架" value="1"></el-option>
            <el-option label="下架" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠卷发行方式" prop="preferentialWay">
          <el-input v-model="form.preferentialWay" placeholder="请输入优惠卷发行方式" style="width: 100%;" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="优惠卷数量" prop="preferentialQuantity">
          <el-input v-model="form.preferentialQuantity" placeholder="请输入优惠卷数量" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker v-model="form.startTime" type="datetime" placeholder="选择开始时间" style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择结束时间" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- 可选：使用折叠面板来隐藏不常用的字段 -->
    <el-collapse v-model="activeNames">
      <el-collapse-item title="更多选项" name="1">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="创建人" prop="createId">
              <el-input v-model="form.createId" placeholder="请输入创建人" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="创建时间" prop="createDate">
              <el-date-picker v-model="form.createDate" type="datetime" placeholder="选择创建时间" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新人" prop="updateId">
              <el-input v-model="form.updateId" placeholder="请输入更新人" style="width: 100%;" />
            </el-form-item>
            <el-form-item label="更新时间" prop="updateDate">
              <el-date-picker v-model="form.updateDate" type="datetime" placeholder="选择更新时间" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-collapse-item>
    </el-collapse>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitForm">确 定</el-button>
    <el-button @click="cancel">取 消</el-button>
  </div>
</el-dialog>
	<el-dialog :title="title" :visible.sync="viewOpen" width="1000px" append-to-body custom-class="view-dialog">
	      <el-descriptions class="margin-top" :column="3" :size="size" border>
	        <!-- Top Section -->
	        <el-descriptions-item label="优惠卷id">{{ form.preferentialId }}</el-descriptions-item>
	        <el-descriptions-item label="优惠卷名称">{{ form.preferentialName }}</el-descriptions-item>
	        <el-descriptions-item label="优惠卷发行方式">{{ form.preferentialState }}</el-descriptions-item>
	        <el-descriptions-item label="优惠卷数量">{{ form.preferentialQuantity }}</el-descriptions-item>
			<el-descriptions-item label="优惠卷剩余数量">{{ form.preferentialRemainder }}</el-descriptions-item>
	      </el-descriptions>
	      <br />
	      <el-descriptions class="margin-top" :column="2" :size="size" border>
	        <!-- Middle Section -->
	        <el-descriptions-item label="创建人">{{ form.createId }}</el-descriptions-item>
	        <el-descriptions-item label="创建时间">{{ form.createDate }}</el-descriptions-item>
	        <el-descriptions-item label="更新人">{{ form.updateId }}</el-descriptions-item>
	        <el-descriptions-item label="更新时间">{{ form.updateDate }}</el-descriptions-item>
	      </el-descriptions>
	      <div slot="footer" class="dialog-footer">
	        <el-button @click="viewClose">关 闭</el-button>
	      </div>
	    </el-dialog>
  </div>
</template>

<script>
import { listPreferential, getPreferential, delPreferential, addPreferential, updatePreferential } from "@/api/preferential/preferential";

export default {
  name: "Preferential",
  data() {
    return {
		//查看字体大小
		size:'medium',
		//查看层	
		viewOpen: false,
		// 遮罩层
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠卷表格数据
      preferentialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        preferentialId: null,
        preferentialName: null,
        preferentialState: null,
        preferentialWay: null,
        preferentialQuantity: null,
        startTime: null,
        endTime: null,
        preferentialRemainder: null,
        createId: null,
        createDate: null,
        updateId: null,
        updateDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
	  //关闭查看层
	  viewClose(){
	    this.viewOpen = false;
	  },
    /** 查询优惠卷列表 */
    getList() {
      this.loading = true;
      listPreferential(this.queryParams).then(response => {
        this.preferentialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        preferentialId: null,
        preferentialName: null,
        preferentialState: null,
        preferentialWay: null,
        preferentialQuantity: null,
        startTime: null,
        endTime: null,
        preferentialRemainder: null,
        createId: null,
        createDate: null,
        updateId: null,
        updateDate: null
      };
      this.resetForm("form");
    },
	formatState(row) {
		const statusMap = {
			1: '上架',
			2: '下架',
		};
		return statusMap[row.preferentialState] || '未知';
	},
	aaa(row){
		console.log(row)
		this.reset();
		const oederId = row.preferentialId
		getPreferential(oederId).then(response => {
		  this.form = response.data;
		  this.title = "查看";
		  this.viewOpen = true; // 打开查看对话框
		});
	},
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠卷";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPreferential(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠卷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePreferential(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreferential(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除优惠卷编号为"' + ids + '"的数据项？').then(function() {
        return delPreferential(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('preferential/preferential/export', {
        ...this.queryParams
      }, `preferential_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
